package com.thinkfit.jy.view;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;


public class LeftSlopSetView implements View.OnClickListener {

    private static LeftSlopSetView leftSlopSetView;
    WindowManager windowManager;
    Context mContext;
    View inflate;
    boolean isFist = true;
    ConstraintLayout clBg;
    TextView tvItem1,tvItem2,tvItem3,tvItem4;
    boolean isSlop = false;
    boolean isLongClick = false;
    int slopItem = 0;

    public static LeftSlopSetView getInstance(){
        if(leftSlopSetView == null){
            leftSlopSetView = new LeftSlopSetView();
        }
        return leftSlopSetView;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }
        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_right_slop_set, null);
            clBg = inflate.findViewById(R.id.cl_bg);
            tvItem1 = inflate.findViewById(R.id.tv_item1);
            tvItem2 = inflate.findViewById(R.id.tv_item2);
            tvItem3 = inflate.findViewById(R.id.tv_item3);
            tvItem4 = inflate.findViewById(R.id.tv_item4);

            tvItem1.setOnClickListener(this);
            tvItem2.setOnClickListener(this);
            tvItem3.setOnClickListener(this);
            tvItem4.setOnClickListener(this);
            clBg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goneLayout();
                }
            });
        }




        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.CENTER;

        System.out.println("ddddddddddddddddddddddd  isFist : "+isFist);

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            System.out.println("ddddddddddddddddddddddd  inflate : "+(inflate == null));
            inflate.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_item1:
                slopItem = 3;
                setSlop(slopItem);
                break;
            case R.id.tv_item2:
                slopItem = 6;
                setSlop(slopItem);
                break;
            case R.id.tv_item3:
                slopItem = 9;
                setSlop(slopItem);
                break;
            case R.id.tv_item4:
                slopItem = 12;
                setSlop(slopItem);
                break;
        }
        goneLayout();
    }


    public void setSlop(int slop){
//        int speed = MotionDataUtils.getInstance().getCurrentInfo().speed;
        int speed = MotionDataUtils.getInstance().deviceSpeed;
        isSlop = false;
        if(DeviceTypeUtils.connectDeviceType() == 0){
            if (MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
                if (slop < 0) {
                    return;
                }
                BluetoothOrder.getInstance().setTreadmillSpeedAndSlope(speed,slop);

            }else {

                if (slop < 0 || slop > 32) {
                    return;
                }
                int slopNum = MotionDataUtils.getInstance().getCurrentInfo().slop;
                BluetoothOrder.getInstance().setNowelAndSlope(slop, slopNum);
            }
        }else {
            if (MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)) {
                if (slop < 0) {
                    return;
                }
                System.out.println("ddddddddddddddddddddddd    sendMsg1111 :  slop : "+slop+"   speed : "+speed);
                SerialPortManager.getInstance().setSpeedSlop(speed, slop);
            } else {
                if (slop < 0 || slop > 32) {
                    return;
                }
                System.out.println("ddddddddddddddddddddddd    sendMsg2222 :  slop : "+slop);
                int slopNum = MotionDataUtils.getInstance().getCurrentInfo().slop;
                SerialPortManager.getInstance().setResistanceSlop(slop, slopNum);
            }
        }
    }




    public void goneLayout(){
        if(inflate != null){
            inflate.setVisibility(View.GONE);
        }
    }
    /**
     * dp2px
     */
    protected int dp2px(float dpValue) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }

}
